//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.Text;
using Microsoft.Research.ScientificWorkflow.TridentUtilities;
using System.Windows.Media.Imaging;
using System.Collections.Generic;

namespace WorkflowApplicationModels
{
    /// <summary>
    /// The workflow model.
    /// </summary>
    public class BaseModel : INotifyPropertyChanged
    {
        #region Private Variables

        private string name;

        private string label;

        private Guid id;

        private bool isSelected;

        #endregion Private Variables

        #region Public Properties

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                this.name = value;
                this.RaisePropertyChanged("Name");
            }
        }

        /// <summary>
        /// Gets or sets the label.
        /// </summary>
        /// <value>The label.</value>
        public string Label
        {
            get
            {
                return this.label;
            }

            set
            {
                this.label = value;
                this.RaisePropertyChanged("Label");
            }
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public Guid Id
        {
            get
            {
                return this.id;
            }

            set
            {
                this.id = value;
                this.RaisePropertyChanged("Id");
            }
        }

        /// <summary>
        /// Gets or sets the item icon.
        /// </summary>
        /// <value>The item icon.</value>
        public BitmapImage ItemIcon { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is selected.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is selected; otherwise, <c>false</c>.
        /// </value>
        public bool IsSelected
        {
            get
            {
                return this.isSelected;
            }

            set
            {
                this.isSelected = value;
                this.RaisePropertyChanged("IsSelected");
            }
        }

        #endregion Public Properties

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the property changed.
        /// </summary>
        /// <param name="name">The name.</param>
        protected void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// Creates the image.
        /// </summary>
        /// <param name="imagePath">The image path.</param>
        /// <returns></returns>
        public static BitmapImage CreateImage(string imagePath)
        {
            BitmapImage image = null;
            try
            {
                image = new BitmapImage(new Uri(imagePath, UriKind.Absolute));
            }
            catch (Exception ex)
            {
                TridentErrorHandler.HandleUIException(ex);
            }

            return image;
        }

        /// <summary>
        /// Gets the model by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="coll">The collection.</param>
        /// <returns></returns>
        public static BaseModel GetModelById(Guid id, IEnumerable<BaseModel> collection)
        {
            BaseModel model = null;
            if (!id.Equals(Guid.Empty))
            {
                foreach (BaseModel currentModel in collection)
                {
                    if (currentModel.Id.Equals(id))
                    {
                        model = currentModel;
                        break;
                    }
                    else
                    {
                        CategoryModel categoryModel = currentModel as CategoryModel;
                        if (categoryModel != null)
                        {
                            model = BaseModel.GetModelById(id, categoryModel.Children);
                            if (model != null)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return model;
        }

        #endregion Public Methods
    }
}
